/*
 * Created on Jul 22, 2005
 *
 */
package com.erclab.internal.xpresso.reports.filters;

import java.util.HashMap;

import com.erclab.internal.xpresso.dataAccess.Query;
import com.erclab.internal.xpresso.reports.Report;
import com.erclab.internal.xpresso.reports.ReportFilter;
import com.erclab.internal.xpresso.util.MysqlFormatter;
import com.erclab.internal.xpresso.webapp.HTMLHelper;

/**
 * @author Enrique Rodriguez.
 *
 */
public class MysqlDateRangeFilter extends ReportFilter {

	/* (non-Javadoc)
	 * @see com.erclab.internal.xpresso.reports.ReportFilter#applyFilterToQuery(com.erclab.internal.xpresso.dataAccess.Query, java.util.HashMap)
	 */
	public void applyFilterToQuery(Query query, HashMap conditions) {
		String date1 = ((String[])conditions.get("Filter" +  this.columnToFilter))[0];
		String date2 = ((String[])conditions.get("Filter" +  this.columnToFilter + "2"))[0];
		String conditionType =	(String)conditions.get("Filter" +  this.columnToFilter + "Condition");
		
		if (date1 != null && !date1.equals("") && date2 != null &&! date2.equals("")){
			StringBuffer theQuery = new StringBuffer(query.getQuery());
			if (theQuery.indexOf("WHERE") > 0){
				theQuery.append(" AND " + this.columnToFilter + " BETWEEN ? AND ?");
			} else {
				theQuery.append(" WHERE " + this.columnToFilter + " BETWEEN ? AND ?" );
			}
			query.setQuery(theQuery.toString());
			
			query.addValueToQuery(MysqlFormatter.convertDateToMySqlFormat(date1));
			query.addValueToQuery(MysqlFormatter.convertDateToMySqlFormat(date2));
		}
		
	}

	/* (non-Javadoc)
	 * @see com.erclab.internal.xpresso.reports.ReportFilter#conditionTypesToHTML(com.erclab.internal.xpresso.reports.Report)
	 */
	public String conditionTypesToHTML(Report theReport) {
		return "";
	}

	/* (non-Javadoc)
	 * @see com.erclab.internal.xpresso.reports.ReportFilter#filterBoxToHTML(com.erclab.internal.xpresso.reports.Report)
	 */
	public String filterBoxToHTML(Report theReport) {
		StringBuffer sb = new StringBuffer();
		sb.append(HTMLHelper.getCalendarBox("Filter" + this.columnToFilter, 
											"Filter" + this.columnToFilter,
											null,
											theReport.getReportName() + "Filters"));
		sb.append(this.getParameters());
		sb.append(HTMLHelper.getCalendarBox("Filter" + this.columnToFilter + "2", 
											"Filter" + this.columnToFilter + "2",
											null,
											theReport.getReportName() + "Filters"));
		
		return sb.toString();
	}

}
